package com.example.android.chartapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

public class ChartRect extends View {

    private static final int LEFT = 0, TOP = 0, RIGHT = 400, BOTTOM = 300;
    private float max = 100;
    private float startX = 0;
    private ArrayList<Float> datas = new ArrayList<>();

    public ChartRect(Context context) {
        super(context);
    }

    public ChartRect(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChartRect(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void addData(float data) {
        datas.add(data);
        postInvalidate();
    }

    Paint paint = new Paint();

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setAntiAlias(true);

        canvas.drawLine(LEFT + 20, TOP, LEFT + 20, BOTTOM, paint);
        canvas.drawLine(LEFT, BOTTOM - 20, RIGHT, BOTTOM - 20, paint);

        for (int i = 1; i < 5; i++) {
            float y = BOTTOM - (BOTTOM - 30) / 4 * i - 20;
            canvas.drawLine(LEFT + 10, y, LEFT + 20, y, paint);
            canvas.drawText((max / 4 * i) + "", LEFT, y + 10, paint);
        }
    }

    Paint contentPaint = new Paint();

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        canvas.translate(startX, TOP);

        contentPaint.setAntiAlias(true);
        contentPaint.setStyle(Paint.Style.STROKE);

        for (int i = 0; i < (datas.size() > 7 ? datas.size() : 7); i++) {
            float x = (RIGHT - 30) / 6 * (i+1) + 20;
            canvas.drawLine(x, BOTTOM - 20, x, BOTTOM - 10, paint);
            canvas.drawText((i + 1) + "", x, BOTTOM, paint);
        }

        for (int i = 0; i < datas.size(); i++) {
            float x = (RIGHT - 30) / 6 * (i+1) + 20;
            float y = BOTTOM - datas.get(i) / max * (BOTTOM - 30) - 20;

            canvas.drawRect(x - 8, y, x + 8, BOTTOM - 20, contentPaint);
        }

        canvas.restore();
    }

    float beginX = 0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                beginX = event.getX();
                Log.i("test-log", beginX+"");
                break;
            case MotionEvent.ACTION_MOVE:
                float dis = event.getX() - beginX;
                beginX = event.getX();
                float temp = startX + dis;
                Log.i("test", temp+"");

                if (temp > 0) return false;
                if (LEFT + 20 + (RIGHT - 20) / 6 * datas.size() + temp < RIGHT - 40) return false;
                startX = temp;
                break;
        }
        postInvalidate();
        return true;
    }

}
